package ck.encrypt;

import org.apache.tomcat.util.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class AesUtil {
    public static final String bm = "UTF-8";

    private String vipara;
    private String aseKey;

    public AesUtil(String vipAra, String aesKey) {
        this.vipara = vipAra;
        this.aseKey = aesKey;
    }

    public String encrypt(String cleartext) {
        //加密方式： AES128(CBC/PKCS5Padding) + Base64, 私钥：aabbccddeeffgghh
        try {
            IvParameterSpec zeroIv = new IvParameterSpec(vipara.getBytes());
            //两个参数，第一个为私钥字节数组， 第二个为加密方式 AES或者DES
            SecretKeySpec key = new SecretKeySpec(aseKey.getBytes(), "AES");
            //实例化加密类，参数为加密方式，要写全
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); //PKCS5Padding比PKCS7Padding效率高，PKCS7Padding可支持IOS加解密
            //初始化，此方法可以采用三种方式，按加密算法要求来添加。
            // （1）无第三个参数
            // （2）第三个参数为SecureRandom random = new SecureRandom();中random对象，随机数。(AES不可采用这种方法)
            // （3）采用此代码中的IVParameterSpec
            cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
            //加密操作,返回加密后的字节数组，然后需要编码。主要编解码方式有Base64, HEX, UUE,7bit等等。此处看服务器需要什么编码方式
            byte[] encryptedData = cipher.doFinal(cleartext.getBytes(bm));

            //return new BASE64Encoder().encode(encryptedData);
            return new Base64().encodeAsString(encryptedData);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public String decrypt(String encrypted) {
        try {
            byte[] byteMi = new Base64().decode(encrypted);
            IvParameterSpec zeroIv = new IvParameterSpec(vipara.getBytes());
            SecretKeySpec key = new SecretKeySpec(
                    aseKey.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            //与加密时不同MODE:Cipher.DECRYPT_MODEya
            cipher.init(Cipher.DECRYPT_MODE, key, zeroIv);
            byte[] decryptedData = cipher.doFinal(byteMi);
            return new String(decryptedData, bm);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static void main(String[] args) {
        String content = "TuTuk+xxx+yyy";
        AesUtil ctAes = new AesUtil("5BE60E709E4724C7", "49BA59ABBE56E057");
//        // 加密
        System.out.println("加密前：" + content);
        String encryptResult = ctAes.encrypt(content);
//
        System.out.println("加密后：" + encryptResult);
        // 解密

        String decryptResult = ctAes.decrypt(encryptResult);
        System.out.println("解密后：" + decryptResult);
    }
}
